const state = {
  username: "test",
  isLogin: true,
  userlist: [
    {
      index: 0,
      name: "test",
      pwd: "123456",
    },
  ],
};
const getters = {
  isLogin(state) {
    return state.isLogin;
  },
  userName(state) {
    return state.username;
  },
};
const actions = {};
const mutations = {
  register(state, [name, pwd]) {
    let temp = {};
    temp.index = state.userlist.length;
    temp.name = name;
    temp.pwd = pwd;
    state.userlist.push(temp);
  },
  logout(state) {
    state.username = "";
    state.isLogin = false;
  },
  login(state, [name, pwd]) {
    let isFind = false;
    state.userlist.forEach((element) => {
      if (element.name == name && element.pwd == pwd) {
        isFind = true;
      }
    });
    if (isFind) {
      state.username = name;
      state.isLogin = true;
    }
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
